package com.kiseny.main;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.common.fileFilter.JpgFileFilter;
import com.common.fileFilter.NotSVNFileFilter;
import com.common.fileFilter.TxtFileFilter;
import com.common.util.Constants;

public class AddFriendsOnSina {

	//private static final String destFileName = "D:/svn2/shop2011/doc/dest.properties";
	private static final String dataPath = "D:/svn2/morgan0329/doc2/yunbei/";
	
	private static final String uploadTool = "D:/svn2/shop2011/exe/upload.exe";
	
	private static final int waitTime = 5000;


	public static void main(String[] args) throws Exception {

		WebDriver driver = new FirefoxDriver();
		
		driver.get("http://t.sina.com.cn/morgan0329/");
		
		//Step1: login
	    driver.findElement(By.className("r_loginbtn")).click();
	    
		
		
		WebElement follow = driver.findElement(By.id("attentions"));
		
		if (follow != null) {
			follow.click();
		}
		List<WebElement> friendList = driver.findElements(By.className("conBox_c"));
		
		for (WebElement friend : friendList) {

			List<WebElement> links = friend.findElements(By.tagName("a"));
			String url = links.get(0).getAttribute("href");
			String uid = links.get(0).getAttribute("uid");
			String name = links.get(0).getText();
			
			//String string = friend.findElement(By.className("address")).getValue();
			
			OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream("D:/svn/Sina/" + uid + name + ".txt"), "UTF-8");
			BufferedWriter bufferedWriter = new BufferedWriter(write);
			
			bufferedWriter.write("URL=" + url + Constants.MARKS_WIN_LF);
			bufferedWriter.close();
		}
		
		//step0: Login to taobao
		driver.get("https://login.taobao.com/member/login.jhtml?f=top&redirectURL=http%3A%2F%2Fwww.taobao.com%2F");
		List<WebElement> loginTexts = driver.findElements(By.className("login-text"));
		loginTexts.get(0).sendKeys("  zjxiao5");
		driver.findElement(By.id("J_SafeLoginCheck")).click();
		loginTexts.get(1).sendKeys("p@ssword1");
		List<WebElement> eleLogins = driver.findElements(By.className("J_Submit"));
		eleLogins.get(0).click();

		File rootFile = new File(dataPath);
		File[] folders = rootFile.listFiles(new NotSVNFileFilter());
		Properties prop2 = new Properties();
		for (File folder : folders) {
		     if (folder.isDirectory()) {
		    	 
		    	 String folderName = folder.getName();
		    	 
		    	 File[] txtFiles = folder.listFiles(new TxtFileFilter());
		    	 File[] jpgFiles = folder.listFiles(new JpgFileFilter());
		    	 
		    	 for (File file : txtFiles) {
		    		
		    		prop2.clear();
		    		prop2.load(new FileReader(file));
		    		
		 			//step1: Go to upload page.
		 			driver.get("http://upload.taobao.com/auction/publish/publish.htm?auction_type=b&auth_alert=true&isAliMall=false");
		 			
		 			Thread.sleep(waitTime);
		 	        WebElement selectElement = driver.findElement(By.id("J_SelOftenSelected"));
		 	        List<WebElement> selectElementOptions = selectElement.findElements(By.tagName("option"));
		 	        for (WebElement selectElementOption : selectElementOptions) {
		 	         if (selectElementOption.getAttribute("value").equals("50022568")) {
		 	           selectElementOption.setSelected();
		 	         }
		 	        }
		 	        Thread.sleep(waitTime);
		 	        driver.findElement(By.id("J_BtnPublish")).click();
		 	        
		 	        //step2: Fill table with data
		 	        Thread.sleep(waitTime);
		 	        driver.findElement(By.id("on1")).click();
		 	        
		 	        driver.findElement(By.id("TitleID")).sendKeys(((String) prop2.get("ProductNameCN")).replace("云贝", "Kiseny"));
		 	        driver.findElement(By.id("buynow")).sendKeys(((String) prop2.get("taobaoPrice")).replace("RMB", ""));
		 	        driver.findElement(By.id("outerIdId")).sendKeys((String) prop2.get("ProductCode"));
		 	        driver.findElement(By.id("quantityId")).clear();
		 	        driver.findElement(By.id("quantityId")).sendKeys("999"); //Hard code: 999
		 	       
		 	        List<File> jpgFileList = new ArrayList<File>();
		 	        for (File jpgFile : jpgFiles) {
		 	        	if(jpgFile.getName().startsWith((String) prop2.get("ProductCode"))) {
		 	        		jpgFileList.add(jpgFile);
		 	        	}
		 	        }
		 	        
		 	        for(int i=0; i< jpgFileList.size(); i++) {
			 			try {
			 				if (i == 0) {
				 				Runtime.getRuntime().exec(uploadTool + " " + folder.getPath() 
				 						+ "\\" + (String) prop2.get("ProductCode") + Constants.MARKS_DASH + i + ".jpg");
				 				driver.findElement(By.name("_fma.pu._0.ima")).click();
				 				
			 				} else if (i == 1) {
				 				Runtime.getRuntime().exec(uploadTool + " " + folder.getPath() 
				 						+ "\\" + (String) prop2.get("ProductCode") + Constants.MARKS_DASH + i + ".jpg");
				 				driver.findElement(By.name("_fma.pu._0.imag")).click();
				 				
			 				} else if (i == 2) {
				 				Runtime.getRuntime().exec(uploadTool + " " + folder.getPath() 
				 						+ "\\" + (String) prop2.get("ProductCode") + Constants.MARKS_DASH + i + ".jpg");
				 				driver.findElement(By.name("_fma.pu._0.image")).click();
				 				
			 				} else if (i == 3) {
				 				Runtime.getRuntime().exec(uploadTool + " " + folder.getPath() 
				 						+ "\\" + (String) prop2.get("ProductCode") + Constants.MARKS_DASH + i + ".jpg");
				 				driver.findElement(By.name("_fma.pu._0.image4")).click();
			 					
			 				} else if (i == 4) {
				 				Runtime.getRuntime().exec(uploadTool + " " + folder.getPath() 
				 						+ "\\" + (String) prop2.get("ProductCode") + Constants.MARKS_DASH + i + ".jpg");
				 				driver.findElement(By.name("_fma.pu._0.image4")).click();
				 				
			 				} else {
			 					//Empty
			 				}

			 			} catch (Exception e) {
			 				e.printStackTrace();
			 			}
		 	        }

		 	        StringBuffer sb = new StringBuffer("   " + ((String) prop2.get("DetailsCN")).replace("云贝", "Kiseny") + Constants.MARKS_WIN_LF);
		 	        if (prop2.get("MaterialCN") != null) {
		 	        	sb.append("材质：" + (String) prop2.get("MaterialCN") + "\n");
		 	        }
		 	        if (prop2.get("SpecificationsCN") != null) {
		 	        	sb.append("规格：" + (String) prop2.get("SpecificationsCN") + "\n");
		 	        }
		 	        if (prop2.get("NotesCN") != null) {
		 	        	sb.append("备注：" + ((String) prop2.get("NotesCN")).replace("云贝", "Kiseny") + "\n");
		 	        }
		 	        driver.findElement(By.id("newEditorContainer"))
                         .findElement(By.tagName("iframe")).sendKeys(sb.toString());
		 	        
		 	        WebElement eleShopCatList = driver.findElement(By.className("shop-cat-list"));
		 	        List<WebElement> eleLis = eleShopCatList.findElements(By.tagName("li"));
		 	        for (WebElement eleLi: eleLis) {
		 	        	if (eleLi.findElement(By.tagName("label")).getText().equalsIgnoreCase(folderName)) {
		 	        		    eleLi.findElement(By.tagName("input")).clear();
			 	        		eleLi.findElement(By.tagName("input")).click();
		 	        	}
		 	        }
		 	        
		 	        //Location
		 	        WebElement selectLocation = driver.findElement(By.id("J_province"));
		 	        List<WebElement> selectLocationOptions = selectLocation.findElements(By.tagName("option"));
		 	        for (WebElement selectLocationOption : selectLocationOptions) {
		 	         if (selectLocationOption.getAttribute("value").equals("云南")) {
		 	        	 selectLocationOption.setSelected();
		 	         }
		 	        }
		 	        
					//driver.findElement(By.id("whopsBuyer")).clear();
					//driver.findElement(By.id("whopsBuyer")).click();

					driver.findElement(By.id("isApplyPostage_false")).clear();
					driver.findElement(By.id("isApplyPostage_false")).click();

					driver.findElement(By.id("pyTF")).clear();
					driver.findElement(By.id("pyTF")).sendKeys("5.0");
					driver.findElement(By.id("kdTF")).clear();
					driver.findElement(By.id("kdTF")).sendKeys("10.0");
					driver.findElement(By.id("emsTF")).clear();
					driver.findElement(By.id("emsTF")).sendKeys("20.0");
		 	        
		 	        driver.findElement(By.id("durationId14")).click();

		 	        //driver.findElement(By.id("J_SelectAfterSale")).click();
		 	        driver.findElement(By.id("event_submit_do_publish")).click();
		 	        if (driver.findElement(By.id("event_submit_do_publish")) != null) {
			 	        driver.findElement(By.id("event_submit_do_publish")).click();
		 	        }

				}
			} else {
				// Empty
			}
		}

	}
}